﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit
{
    public partial class UCTeamData : UserControl
    {
        List<Team> teams;
        List<Magic> magics;
        PListRoot root;
        String cczDirector;
        String resourcesDirect = ""+Form1.cczDirector;
        public UCTeamData()
        {
            InitializeComponent();
        }
        public UCTeamData(String cczDirector_)
        {
            InitializeComponent();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);
            cczDirector = cczDirector_;
            teams = new List<Team>();
            magics = new List<Magic>();
            LoadMagicData(cczDirector+"\\MagicData.plist");
            LoadTeamData(cczDirector + "\\TeamData.plist");
            teams.Sort(Team.compare);
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                comboBoxRangeAttack.Items.Add(kv.Value);
            }
            foreach (KeyValuePair<int, String> kv in Constant.weaponTypeDic)
            {
                comboBoxWeapon.Items.Add(kv.Value);
            }
            foreach (KeyValuePair<int, String> kv in Constant.armorTypeDic)
            {
                comboBoxArmor.Items.Add(kv.Value);
            }
            foreach (KeyValuePair<int, String> kv in Constant.teamTypeDic)
            {
                comboBoxType.Items.Add(kv.Value);
            }

            foreach (Magic magic in magics)
            {
                ColumnMagic.Items.Add(magic.name);
            }
            ShowTeams();
        }
        private void LoadMagicData(String fileName_)
        {

            root = PListRoot.Load(fileName_);
            PListDict rootDic = (PListDict)root.Root;
            PListDict magicsDic = (PListDict)rootDic["Magics"];
            foreach (KeyValuePair<String, IPListElement> a in magicsDic)
            {
                PListDict magicDic = (PListDict)a.Value;
                PListString name = (PListString)magicDic["Name"];
                PListInteger num = (PListInteger)magicDic["Num"];
                PListInteger numIcon = (PListInteger)magicDic["NumIcon"];
                PListString dspt = (PListString)magicDic["Dspt"];
                PListInteger numEffect = (PListInteger)magicDic["NumEffect"];
                PListInteger type = (PListInteger)magicDic["Type"];
                PListInteger target = (PListInteger)magicDic["Target"];
                PListInteger consume = (PListInteger)magicDic["Consume"];
                PListInteger numAnimCall = (PListInteger)magicDic["NumAnimCall"];
                PListInteger numAnimMeff = (PListInteger)magicDic["NumAnimMeff"];
                PListInteger numAnimSeff = (PListInteger)magicDic["NumAnimSeff"];
                PListInteger numRangeAttack = (PListInteger)magicDic["NumRangeAttack"];
                PListInteger numRangeEffect = (PListInteger)magicDic["NumRangeEffect"];
                PListInteger valueAlpha = (PListInteger)magicDic["ValueAlpha"];
                PListInteger valueC = (PListInteger)magicDic["ValueC"];

                Magic magic = new Magic();
                magic.name = name.Value;
                magic.num = num.Value;
                magic.dspt = dspt.Value;
                magic.numEffect = numEffect.Value;
                magic.numIcon = Convert.ToInt64(numIcon.Value);
                magic.type = type.Value;
                magic.target = target.Value;
                magic.consume = consume.Value;
                magic.numAnimCall = numAnimCall.Value;
                magic.numAnimMeff = numAnimMeff.Value;
                magic.numAnimSeff = numAnimSeff.Value;
                magic.numRangeAttack = numRangeAttack.Value;
                magic.numRangeEffect = numRangeEffect.Value;
                magic.valueAlpha = valueAlpha.Value;
                magic.valueC = valueC.Value;
                magics.Add(magic);

            }
            magics.Sort(Magic.compare);

        }
        private void LoadTeamData(String fileName_)
        {
            root = PListRoot.Load(fileName_);
            PListDict rootDic = (PListDict)root.Root;
            PListDict teamsDic = (PListDict)rootDic["Teams"];
            foreach (KeyValuePair<String, IPListElement> a in teamsDic)
            {
                PListDict teamDic = (PListDict)a.Value;
                PListString name = (PListString)teamDic["Name"];
                PListInteger num = (PListInteger)teamDic["Num"];
                PListString dspt = (PListString)teamDic["Dspt"];
                PListInteger type = (PListInteger)teamDic["Type"];
                PListInteger numAttackRange = (PListInteger)teamDic["NumAttackRange"];
                PListInteger weaponType = (PListInteger)teamDic["WeaponType"];
                PListInteger armorType = (PListInteger)teamDic["ArmorType"];
                PListInteger attack = (PListInteger)teamDic["Attack"];
                PListInteger defend = (PListInteger)teamDic["Defend"];
                PListInteger willpower = (PListInteger)teamDic["Willpower"];
                PListInteger Erupt = (PListInteger)teamDic["Erupt"];
                PListInteger morale = (PListInteger)teamDic["Morale"];
                PListInteger move = (PListInteger)teamDic["Move"];
                PListInteger HPDelta = (PListInteger)teamDic["HPDelta"];
                PListInteger MPDelta = (PListInteger)teamDic["MPDelta"];
                PListInteger level = (PListInteger)teamDic["Level"];

                PListDict learnMagicLevel = (PListDict)teamDic["LearnMagicLevel"];

                Team team = new Team();
                team.name = name.Value;
                team.num = num.Value;
                team.dspt = dspt.Value;
                team.type = type.Value;
                team.numAttackRange = numAttackRange.Value;
                team.weaponType = weaponType;
                team.armorType = armorType;
                team.attack = attack;
                team.defend = defend;
                team.willpower = willpower;
                team.erupt = Erupt;
                team.morale = morale;
                team.move = move;
                team.HPDelta = HPDelta;
                team.MPDelta = MPDelta;
                team.level = level;
                foreach (KeyValuePair<String, IPListElement> l in learnMagicLevel)
                {
                    PListInteger value = (PListInteger)l.Value;
                    team.learnMagicLevel.Add(Convert.ToInt64(l.Key), value.Value);
                }
                teams.Add(team);

            }
        }
        public void SaveAllData()
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            SaveTeamDetail(Convert.ToInt32(item.SubItems[0].Text));
            root.Save(cczDirector + "\\TeamData.plist");
        }
        private void ShowTeams()
        {
            listView1.Clear();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);

            foreach (Team team in teams)
            {
                ListViewItem item = new ListViewItem(new String[] { team.num + "", team.name });
                listView1.Items.Add(item);
            }
            listView1.Focus();
            listView1.Items[0].Selected = true;
            
        }
        private void ShowTeamDetail(int ID)
        {
            Team team = teams.Find((t) => { return t.num == ID; });
            textBoxName.Text = team.name;
            textBoxAttack.Text = team.attack.ToString();
            textBoxDefend.Text = team.defend.ToString();
            textBoxWillpower.Text = team.willpower.ToString();
            textBoxErupt.Text = team.erupt.ToString(); ;
            textBoxMorale.Text = team.morale.ToString(); ;
            textBoxMove.Text = team.move.ToString();
            textBoxHP.Text = team.HPDelta.ToString() ;
            textBoxMP.Text = team.MPDelta.ToString();
            textBoxLevel.Text = team.level.ToString();
            textBoxDspt.Text = team.dspt;
            comboBoxWeapon.Text = Constant.weaponTypeDic[(int)team.weaponType];
            comboBoxArmor.Text = Constant.armorTypeDic[(int)team.armorType];
            comboBoxRangeAttack.Text = Constant.markTypeDic[(int)team.numAttackRange];
            comboBoxType.Text = Constant.teamTypeDic[(int)team.type];
            dataGridViewMagic.Rows.Clear();
            foreach (KeyValuePair<long, long> kv in team.learnMagicLevel)
            {
                Magic magic = magics.Find((m) => { return m.num == kv.Key; });
                int index = this.dataGridViewMagic.Rows.Add();

                this.dataGridViewMagic.Rows[index].Cells[0].Value = magic.name;
                this.dataGridViewMagic.Rows[index].Cells[1].Value = kv.Value;
            }

            
        }
        private void SaveTeamDetail(int ID)
        {
            Team team = teams.Find((t) => { return t.num == ID; });
            team.name = textBoxName.Text;
            team.dspt = textBoxDspt.Text;
            team.attack = Convert.ToInt64(textBoxAttack.Text);
            team.defend = Convert.ToInt64(textBoxDefend.Text);
            team.willpower = Convert.ToInt64(textBoxWillpower.Text);
            team.erupt = Convert.ToInt64(textBoxErupt.Text);
            team.morale = Convert.ToInt64(textBoxMorale.Text);
            team.move = Convert.ToInt64(textBoxMove.Text);
            team.HPDelta = Convert.ToInt64(textBoxHP.Text);
            team.MPDelta = Convert.ToInt64(textBoxMP.Text);
            team.level = Convert.ToInt64(textBoxLevel.Text);
            foreach (KeyValuePair<int, String> kv in Constant.weaponTypeDic)
            {
                if (kv.Value == comboBoxWeapon.Text)
                {
                    team.weaponType = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.armorTypeDic)
            {
                if (kv.Value == comboBoxArmor.Text)
                {
                    team.armorType = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                if (kv.Value == comboBoxRangeAttack.Text)
                {
                    team.numAttackRange = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.teamTypeDic)
            {
                if (kv.Value == comboBoxType.Text)
                {
                    team.type = kv.Key;
                }
            }
            PListDict rootDic = (PListDict)root.Root;
            PListDict teamsDic = (PListDict)rootDic["Teams"];
            foreach (KeyValuePair<String, IPListElement> a in teamsDic)
            {
                PListDict teamDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)teamDic["Num"];
                if (num.Value == team.num)
                {
                    teamDic["Name"] = new PListString(team.name);
                    teamDic["Dspt"] = new PListString(team.dspt);
                    teamDic["Type"] = new PListInteger(team.type);
                    teamDic["HPDelta"] = new PListInteger(team.HPDelta);
                    teamDic["MPDelta"] = new PListInteger(team.MPDelta);
                    teamDic["Attack"] = new PListInteger(team.attack);
                    teamDic["Defend"] = new PListInteger(team.defend);
                    teamDic["Willpower"] = new PListInteger(team.willpower);
                    teamDic["Erupt"] = new PListInteger(team.erupt);
                    teamDic["Morale"] = new PListInteger(team.morale);
                    teamDic["Move"] = new PListInteger(team.move);
                    teamDic["Level"] = new PListInteger(team.level);
                    teamDic["NumAttackRange"] = new PListInteger(team.numAttackRange);
                    teamDic["WeaponType"] = new PListInteger(team.weaponType);
                    teamDic["ArmorType"] = new PListInteger(team.armorType);
                    PListDict lml = new PListDict();
                    for (int i = 0; i <= dataGridViewMagic.Rows.Count - 2;++i )
                    {
                            DataGridViewRow row = dataGridViewMagic.Rows[i];
                            Magic magic = magics.Find((m) => { return m.name == row.Cells[0].Value.ToString(); });
                            lml.Add(magic.num.ToString(), new PListInteger(Convert.ToInt64(row.Cells[1].Value)));
                            
                    }
                    teamDic["LearnMagicLevel"] = lml;
                    team.learnMagicLevel.Clear();
                    foreach (KeyValuePair<String, IPListElement> l in lml)
                    {
                        
                        PListInteger value = (PListInteger)l.Value;
                        team.learnMagicLevel.Add(Convert.ToInt64(l.Key), value.Value);
                    }


                }
            }
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            ShowTeamDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void comboBoxRangeAttack_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                if (kv.Value == comboBoxRangeAttack.Text)
                {
                    pictureBoxAttackRange.Image = Image.FromFile(resourcesDirect + "\\Hitarea\\HitArea" + kv.Key + ".png");

                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            SaveTeamDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void buttonRecover_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];

            ShowTeamDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Team team = new Team();
            team.name = textBoxName.Text;
            team.dspt = textBoxDspt.Text;
            team.attack = Convert.ToInt64(textBoxAttack.Text);
            team.defend = Convert.ToInt64(textBoxDefend.Text);
            team.willpower = Convert.ToInt64(textBoxWillpower.Text);
            team.erupt = Convert.ToInt64(textBoxErupt.Text);
            team.morale = Convert.ToInt64(textBoxMorale.Text);
            team.move = Convert.ToInt64(textBoxMove.Text);
            team.HPDelta = Convert.ToInt64(textBoxHP.Text);
            team.MPDelta = Convert.ToInt64(textBoxMP.Text);
            team.level = Convert.ToInt64(textBoxLevel.Text);

            foreach (KeyValuePair<int, String> kv in Constant.weaponTypeDic)
            {
                if (kv.Value == comboBoxWeapon.Text)
                {
                    team.weaponType = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.armorTypeDic)
            {
                if (kv.Value == comboBoxArmor.Text)
                {
                    team.armorType = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                if (kv.Value == comboBoxRangeAttack.Text)
                {
                    team.numAttackRange = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.teamTypeDic)
            {
                if (kv.Value == comboBoxType.Text)
                {
                    team.type = kv.Key;
                }
            }
            teams.Add(team);
            PListDict rootDic = (PListDict)root.Root;
            PListInteger newNum = (PListInteger)rootDic["NewNum"];
            team.num = newNum.Value;
            rootDic["NewNum"] =new PListInteger( team.num + 1);
            PListDict teamsDic = (PListDict)rootDic["Teams"];
            
            PListDict teamDic = new PListDict();
            teamDic["Num"] = new PListInteger(team.num);
            teamDic["Name"] = new PListString(team.name);
            teamDic["Dspt"] = new PListString(team.dspt);
            teamDic["Type"] = new PListInteger(team.type);
            teamDic["HPDelta"] = new PListInteger(team.HPDelta);
            teamDic["MPDelta"] = new PListInteger(team.MPDelta);
            teamDic["Attack"] = new PListInteger(team.attack);
            teamDic["Defend"] = new PListInteger(team.defend);
            teamDic["Willpower"] = new PListInteger(team.willpower);
            teamDic["Erupt"] = new PListInteger(team.erupt);
            teamDic["Morale"] = new PListInteger(team.morale);
            teamDic["Move"] = new PListInteger(team.move);
            teamDic["Level"] = new PListInteger(team.level);
            teamDic["NumAttackRange"] = new PListInteger(team.numAttackRange);
            teamDic["WeaponType"] = new PListInteger(team.weaponType);
            teamDic["ArmorType"] = new PListInteger(team.armorType);
            PListDict lml = new PListDict();
            for (int i = 0; i <= dataGridViewMagic.Rows.Count - 2; ++i)
            {
                DataGridViewRow row = dataGridViewMagic.Rows[i];
                Magic magic = magics.Find((m) => { return m.name == row.Cells[0].Value.ToString(); });
                lml.Add(magic.num.ToString(), new PListInteger(Convert.ToInt64(row.Cells[1].Value)));

            }
            teamDic["LearnMagicLevel"] = lml;
            foreach (KeyValuePair<String, IPListElement> l in lml)
            {
                team.learnMagicLevel.Clear();
                PListInteger value = (PListInteger)l.Value;
                team.learnMagicLevel.Add(Convert.ToInt64(l.Key), value.Value);
            }

            teamsDic.Add("Team"+newNum,teamDic);
                
            
            ShowTeams();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            teams.RemoveAll((m) => { return m.num == (Convert.ToInt32(item.SubItems[0].Text)); });
            PListDict rootDic = (PListDict)root.Root;
            PListDict teamsDic = (PListDict)rootDic["Teams"];
            String teamKey = null;
            foreach (KeyValuePair<String, IPListElement> a in teamsDic)
            {
                PListDict teamDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)teamDic["Num"];
                if (num.Value == Convert.ToInt32(item.SubItems[0].Text))
                {
                    teamKey = a.Key;
                }
            }
            if (teamKey != null)
                teamsDic.Remove(teamKey);
            ShowTeams();
        }
    }
    class Team
    {
        public String name;
        public long num;
        public String dspt;
        public long type;
        public long numAttackRange;
        public long weaponType;
        public long armorType;
        public long attack;
        public long defend;
        public long willpower;
        public long erupt;
        public long morale;
        public long move;
        public long HPDelta;
        public long MPDelta;
        public long level;
        public Dictionary<long, long> learnMagicLevel=new Dictionary<long,long>();
        public static int compare(Team m1, Team m2)
        {
            if (m1.num == m2.num)
                return 0;
            else if (m1.num > m2.num)

                return 1;

            else if (m1.num < m2.num)
                return -1;
            else
                return 0;
        }

    }
}
